package com.example.backend.controller;

import com.example.backend.entity.Business;
import com.example.backend.entity.Vehicle;
import com.example.backend.service.BusinessService;
import com.example.backend.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController //表示真正处理请求的处理器，相当于@Controller+@ResponseBody
@RequestMapping("/business") //标记控制器
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @Autowired
    private VehicleService vehicleService;

    @GetMapping("/list") //取代@RequestMapping，用于请求方法的GET类型
    public List<Business> findAll(){
        return businessService.findAll();
    }

    @PostMapping("/save") //取代@RequestMapping，用于请求方法的POST类型
    public Business save (@RequestBody Business business){
        //用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)GET方式无请求体，
        //所以使用@RequestBody接收数据时，前端不能使用GET方式提交数据，而是用POST方式进行提交。
        return businessService.save(business);
    }

    @GetMapping("/delete")
    public void delete (@Param("id") Integer id){
        businessService.delete(id);
    }

    @PostMapping("/update")
    public Business update(@RequestBody Business business){
        return businessService.update(business);
    }


    @GetMapping("/search")
    public List<Business> findBusinessByName(@Param("key") String key, @Param("value") String value) throws ParseException {
        System.out.println(key);
        System.out.println(value);
        //规范日期输出格式
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        switch (key){
            case "id":
                return businessService.findById(Integer.parseInt(value));
            case "cname":
                return businessService.findByCname(value);
            case "vname":
                return businessService.findByVname(value);
            case "dname":
                return businessService.findByDname(value);
            case "mode":
                return businessService.findByMode(value);
            case "startDate":
                return businessService.findByStartDate(value);
            case "finishDate":
                //Date date1 = sdf.parse(value);
                return businessService.findByFinishDate(value);
            default:break;
        }
        return null;
    }

    //催车还交
    @GetMapping("/urge")
    public String urge(@Param("id") Integer id) throws ParseException {
        //规范日期输出格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        Date date = new Date(System.currentTimeMillis());
        Date d2 = df.parse(df.format(date));
        Date d1 = df.parse(businessService.findById(id).get(0).getFinishDate()) ;
        if(d1.getTime()<d2.getTime()){
            return "已超时，请尽快还车！";
        }
        else{
            long day = (d1.getTime()-d2.getTime())/(24*60*60*1000);
            return "还剩"+day+"天还车。";
        }
    }

    //租金结算
    //租金结算
    @GetMapping("/getrent")
    public double getRent(@Param("id") Integer id) throws ParseException {
        Business business = businessService.findById(id).get(0);
        Integer vid = business.getVid();
        Vehicle v = vehicleService.findById(vid).get(0);
        Integer rent = v.getRent();
        //规范日期输出格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = business.getStartDate();
        String finishDate = business.getFinishDate();
        Date d1 = df.parse(startDate);
        Date d2 = df.parse(finishDate);
        double Rent;
        long days = ((d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000)); //小时数
        if(business.getMode().equals("日租"))
            Rent = days * rent*1.0;
        else
            Rent=days*rent*0.9;
        return Rent;
    }
}
